cmake_minimum_required(VERSION 3.12)
project(ems-cpp-v1 VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")

find_package(PkgConfig REQUIRED)

pkg_check_modules(LIBMODBUS REQUIRED libmodbus)
pkg_check_modules(MOSQUITTO REQUIRED libmosquitto)
find_package(OpenSSL REQUIRED)

find_package(Threads REQUIRED)

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/third_party
    ${LIBMODBUS_INCLUDE_DIRS}
    ${MOSQUITTO_INCLUDE_DIRS}
    ${OPENSSL_INCLUDE_DIR}
)

link_directories(
    ${LIBMODBUS_LIBRARY_DIRS}
    ${MOSQUITTO_LIBRARY_DIRS}
)

file(GLOB_RECURSE SOURCES
    "src/core/*.cpp"
    "src/device/*.cpp"
    "src/protocol/*.cpp"
    "src/service/*.cpp"
    "src/utils/*.cpp"
    "src/web/*.cpp"
)

add_executable(${PROJECT_NAME} src/main.cpp ${SOURCES})

target_link_libraries(${PROJECT_NAME}
    ${LIBMODBUS_LIBRARIES}
    ${MOSQUITTO_LIBRARIES}
    OpenSSL::Crypto
    Threads::Threads
)

target_compile_options(${PROJECT_NAME} PRIVATE
    ${LIBMODBUS_CFLAGS_OTHER}
    ${MOSQUITTO_CFLAGS_OTHER}
)

set_target_properties(${PROJECT_NAME} PROPERTIES
    OUTPUT_NAME "ems-cpp-v1"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_definitions(${PROJECT_NAME} PRIVATE DEBUG=1)
endif()

install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
)

install(DIRECTORY config/
    DESTINATION etc/ems-cpp-v1/
    FILES_MATCHING PATTERN "*.json"
)

enable_testing()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt")
    add_subdirectory(tests)
endif()

# 额外：Modbus 扫描工具
add_executable(scan-modbus src/tools/scan_modbus.cpp src/protocol/modbus/ModbusTCP.cpp src/core/Logger.cpp)
target_include_directories(scan-modbus PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${LIBMODBUS_INCLUDE_DIRS}
)
target_link_libraries(scan-modbus
    ${LIBMODBUS_LIBRARIES}
    Threads::Threads
)
set_target_properties(scan-modbus PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

# 额外：Modbus 测试工具
add_executable(test-modbus src/tools/test_modbus.cpp src/protocol/modbus/ModbusTCP.cpp src/core/Logger.cpp)
target_include_directories(test-modbus PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${LIBMODBUS_INCLUDE_DIRS}
)
target_link_libraries(test-modbus
    ${LIBMODBUS_LIBRARIES}
    Threads::Threads
)
set_target_properties(test-modbus PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

# 额外：MQTT 测试 CLI（不依赖 web/ 源码）
add_executable(mqtt-cli src/tools/mqtt_cli.cpp src/protocol/mqtt/MqttClient.cpp src/utils/Config.cpp src/utils/Json.cpp src/core/Logger.cpp)
target_include_directories(mqtt-cli PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${LIBMODBUS_INCLUDE_DIRS}
    ${MOSQUITTO_INCLUDE_DIRS}
    ${OPENSSL_INCLUDE_DIR}
)
target_link_libraries(mqtt-cli
    ${MOSQUITTO_LIBRARIES}
    OpenSSL::Crypto
    Threads::Threads
)
set_target_properties(mqtt-cli PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

# 额外：MQTT 发布 CLI（按文档构造或原样发布）
add_executable(mqtt-pub src/tools/mqtt_pub.cpp src/protocol/mqtt/MqttClient.cpp src/utils/Config.cpp src/utils/Json.cpp src/core/Logger.cpp)
target_include_directories(mqtt-pub PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${LIBMODBUS_INCLUDE_DIRS}
    ${MOSQUITTO_INCLUDE_DIRS}
    ${OPENSSL_INCLUDE_DIR}
)
target_link_libraries(mqtt-pub
    ${MOSQUITTO_LIBRARIES}
    OpenSSL::Crypto
    Threads::Threads
)
set_target_properties(mqtt-pub PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

# 测试：充电价格跟踪器
add_executable(test-charge-tracker 
    src/tools/test_charge_tracker.cpp 
    src/service/tracker/StorageChargePriceTracker.cpp 
    src/core/Logger.cpp
)
target_include_directories(test-charge-tracker PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_link_libraries(test-charge-tracker
    Threads::Threads
)
set_target_properties(test-charge-tracker PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
